//
//  DYForgetViewController.m
//  DYHospital
//
//  Created by 胡金东 on 16/3/23.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "DYForgetViewController.h"
#import "UIView+Color.h"
#import "DYForgetRegistViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "BSMacros.h"
#import "UIViewController+DYLoadView.h"


@interface DYForgetViewController ()
@property (nonatomic,strong)UITextField *phoneTextField;
@property (nonatomic,strong)UIButton *checkingVerBtn;
@property (nonatomic,strong)UITextField *checkingVerTextField;

@property (nonatomic,strong)UIAlertView *alert;
@property (nonatomic,strong)NSTimer *timer;

@end

@implementation DYForgetViewController{
    int seconds;
}

-(void)viewWillAppear:(BOOL)animated
{
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_Arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnItemClick)];
    backBtnItem.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = backBtnItem;
}
-(void)backBtnItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"忘记密码";
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setTextBtn];
    seconds = 60;
    
}

-(void)setTextBtn
{
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 64 + 25, [UIScreen mainScreen].bounds.size.width - 40, 35)];
    self.phoneTextField.placeholder = @"请输入手机号";
    self.phoneTextField.backgroundColor = [UIColor whiteColor];
    [self.phoneTextField setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    self.phoneTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.phoneTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    [self.view addSubview:self.phoneTextField];
    
    self.checkingVerTextField = [[UITextField alloc]initWithFrame:CGRectMake(20, 64 + 70, [UIScreen mainScreen].bounds.size.width - 40, 35)];
    self.checkingVerTextField.placeholder = @" 请输入验证码";
    self.checkingVerTextField.backgroundColor = [UIColor whiteColor];
    self.checkingVerTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.checkingVerTextField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    [self.checkingVerTextField setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    [self.view addSubview:self.checkingVerTextField];
    
    self.checkingVerBtn = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 150, 64 + 70, 130, 35)];
    [self.checkingVerBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.checkingVerBtn.backgroundColor = [UIColor redColor];
    [self.checkingVerBtn setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    [self.checkingVerBtn addTarget:self action:@selector(checkingVerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.checkingVerBtn];
    
    
    UIButton *forgetBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 64 + 155, [UIScreen mainScreen].bounds.size.width - 40, 40)];
    [forgetBtn setTitle:@"提交" forState:UIControlStateNormal];
    [forgetBtn setRadius:5 borderWith:0 borderColor:[UIColor clearColor]];
    forgetBtn.backgroundColor = [UIColor purpleColor];
    [forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    
    
}

-(void)checkingVerBtnClick
{
    //区号
    NSString* str2 = [NSString stringWithFormat:@"86"];
    if (self.phoneTextField.text.length == 11) {
        
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:str2 customIdentifier:nil result:^(NSError *error) {
            if (!error)
            {
                NSLog(@"验证码发送成功");
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeMin) userInfo:nil repeats:YES];
            }else{
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"codesenderrtitle", nil) message:[NSString stringWithFormat:@"错误描述：%@",[error.userInfo objectForKey:@"getVerificationCode"]] delegate:self cancelButtonTitle:NSLocalizedString(@"sure", nil) otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
        
    }else{
        self.alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号长度不对" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [self.alert show];
        NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeClick) userInfo:nil repeats:YES];
        
        
    }
    

}



-(void)changeMin
{
    if (seconds == 0) {
        [self.checkingVerBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.checkingVerBtn.enabled = YES;
        self.checkingVerBtn.hidden = NO;
        [self.timer invalidate];
        seconds = 60;
        return;
    }
    self.checkingVerBtn.enabled = NO;
    [self.checkingVerBtn setTitle:[NSString stringWithFormat:@"%ds",seconds] forState:UIControlStateNormal];
    seconds --;

}

-(void)timeClick
{
    [self.alert dismissWithClickedButtonIndex:0 animated:YES];

}

-(void)forgetBtnClick
{
    NSString* str2 = [NSString stringWithFormat:@"86"];
    
    [SMSSDK commitVerificationCode:self.checkingVerTextField.text phoneNumber:self.phoneTextField.text zone:str2 result:^(NSError *error) {
        
        if (!error) {
            
           [self showHint:@"验证成功"];
          
           DYForgetRegistViewController *forgetVer = [[DYForgetRegistViewController alloc]init];
         
            [self.navigationController pushViewController:forgetVer animated:YES];
            
        }else{
            
            [self showHint:@"验证码错误" inCenter:YES];
            NSLog(@"错误信息:%@",error);
        }
    }];

    
}


@end
